<?php
session_start();

if (!isset($_SESSION["admin_id"])) {
    header("Location: admin.php");
    exit;
}

$admin_kullanici_adi = $_SESSION["admin_username"] ?? "Yönetici";

// Veritabanı bağlantısı
try {
    $db = new PDO("mysql:host=localhost;dbname=yenicesi_2305610353;charset=utf8", "yenicesi_2305610353", "2305610353C.");
} catch (PDOException $e) {
    die("Veritabanı bağlantı hatası: " . $e->getMessage());
}

// Öğrenci verilerini çek
$sorgu = $db->query("SELECT * FROM ogrenciler", PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Admin Paneli</title>
    <link rel="stylesheet" href="css/admin_dashboard.css">
</head>
<body>

<header>
    👨‍💼 Admin Paneli - Hoş geldin, <?= htmlspecialchars($admin_kullanici_adi) ?>
</header>

<div class="container">

    <h2>Kontrol Paneli</h2>

    <a href="admin_register.php" class="button">Yeni Admin Ekle</a>
	<a href="admin_ogrenci_ekle.php" class="button">Öğrenci Ekle</a>
	<a href="ders_ekle.php" class="button green">Ders Ekle</a>

    <a href="logout.php" class="button">Çıkış Yap</a>

    <h3>Öğrenci Listesi</h3>

    <table>
        <thead>
            <tr>
                <th>Ad Soyad</th>
                <th>ID</th>
                <th>Mail</th>
                <th>Telefon</th>
            </tr>
        </thead>
        <tbody>
    <?php foreach ($sorgu as $ogrenci): ?>
        <tr>
            <td><?= htmlspecialchars($ogrenci["ad"]) ?></td>
            <td><?= htmlspecialchars($ogrenci["id"]) ?></td>
            <td><?= htmlspecialchars($ogrenci["eposta"]) ?></td>
            <td><?= htmlspecialchars($ogrenci["telefon"]) ?></td>
            <td>
    <!-- Silme Formu -->
    <form action="ogrenci_sil.php" method="POST" onsubmit="return confirm('Bu öğrenciyi silmek istediğine emin misin?');" style="display:inline;">
        <input type="hidden" name="ogrenci_id" value="<?= $ogrenci["id"] ?>">
        <button type="submit" class="button red">Sil</button>
		
    </form>

    <!-- Düzenleme Linki -->
    <a href="ogrenci_duzenle.php?id=<?= $ogrenci["id"] ?>" class="button blue">Düzenle</a>
	<!-- Ders Ata Linki -->
    <a href="ders_ata.php?ogrenci_id=<?= $ogrenci["id"] ?>" class="button yellow">Ders Ata</a>
	
</td>
        </tr>
    <?php endforeach; ?>
</tbody>

    </table>

</div>

</body>
</html>
